public class Trans {
    // 字符串变形
    // https://www.nowcoder.com/practice/c3120c1c1bc44ad986259c0cf0f0b80e?tpId=295&tqId=44664&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D295
    private int count;
    public String trans (String s, int n) {
        // write code here
        boolean flg1 = false;
        boolean flg2 = false;
        if (s.charAt(0) == ' ') {
            flg1 = true;
            s = s.substring(1, n);
        }
        if (s.charAt(s.length() - 1) == ' ') {
            flg2 = true;
        }
        String[] ss = s.split(" ");
        if (ss.length == 0) {
            return " " + s;
        }
        swap(ss);
        transfer(ss);
        StringBuilder ret = new StringBuilder(String.join(" ", ss));
        if (flg1) {
            ret.append(" ");
        }
        if (flg2) {
            ret.insert(0, " ");
        }
        return ret.toString();
    }

    private void transfer(String[] ss) {
        for (int i = 0; i < ss.length; i++) {
            char[] c = ss[i].toCharArray();
            StringBuilder sb = new StringBuilder();
            for (char ch : c) {
                if (ch >= 'a' && ch <= 'z') {
                    sb.append((char)(ch - 32));
                } else {
                    sb.append((char)(ch + 32));
                }
                count++;
            }
            ss[i] = sb.toString();
        }
    }

    private void swap(String[] ss) {
        for (int left = 0, right = ss.length - 1; left < right; left++, right--) {
            String tmp = ss[left];
            ss[left] = ss[right];
            ss[right] = tmp;
        }
    }

    public static void main(String[] args) {
        Trans t = new Trans();
        System.out.println(t.trans("    ", 4));
    }
}
